import socket
import sys
import time
import ast

def Verif(mac,rc):#预留验证函数，mac为客户端mac地址，rc为客户机注册码
    print ("客服端mac地址:" ,mac)
    print ("客服端注册码:" ,rc)
    data = input("验证结果:")
    return data

def apply(mac):#申请注册码，mac为客户端mac地址
    print("客服端mac地址:",mac)
    data = input("新注册码:")
    return data




servicesocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
servicesocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host=socket.gethostname()
port=9999
print("等待连接中")
servicesocket.bind((host,port))
print("等待连接中2")
servicesocket.listen(5)
print("等待连接中3")
ClientSock,addr=servicesocket.accept()
print("等待连接")
buf = ClientSock.recv(1024)
if len(buf):
    buff=str(buf,'utf-8')
    #buff="{'state':1,'mac':'40490f2beddd','rc':'aolidhaosfhcalksfh'}"
    buff_dict=ast.literal_eval(buff)
    #print(buff_dict,buff_dict["mac"])
    if (buff_dict['state']==1):#验证
        data = Verif(buff_dict['mac'],buff_dict['rc'])
        jieguo="{'验证结果':'"+data+"'}"
        ClientSock.sendall(jieguo.encode('utf-8'))
    elif (buff_dict['state']==2):
        Nrc=apply(buff_dict['mac'])
        jieguo="{'Nrc':'"+Nrc+"'}"
        ClientSock.sendall(jieguo.encode('utf-8'))
        











